package controllers.session;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import controllers.item.LoginDisplay;

import core.login.LoginSession_WebSocketSessionInterface;

//@Component(value = "loginSession_WebSocketSession")
public class LoginSession_WebSocketSession implements
		LoginSession_WebSocketSessionInterface {
	public final static LoginSession_WebSocketSession session = new LoginSession_WebSocketSession();

	public static LoginSession_WebSocketSession getSession() {
		return session;
	}

	private LoginSession_WebSocketSession() {

	};

	// 登陆者对应一个session
	private final Map<Long, HttpSession> loginId_loginSession = new HashMap<Long, HttpSession>();
	private final Map<Long, Session> loginId_webSocket = new HashMap<Long, Session>();
	private final Map<String, HttpSession> sessionId_session = new HashMap<String, HttpSession>();

	public Map<Long, HttpSession> getLoginId_loginSession() {
		return loginId_loginSession;
	}

	public Map<String, HttpSession> getSessionId_session() {
		return sessionId_session;
	}

	public Map<Long, Session> getLoginId_webSocket() {
		return loginId_webSocket;
	}

	@Override
	public HttpSession getHttpSessionByLid(long id) {
		return this.getLoginId_loginSession().get(id);
	}

	@Override
	public Session getWebSocketSessionByLid(long id) {
		return this.getLoginId_webSocket().get(id);
	}

	@Override
	public void saveLoginSession(long id, HttpSession session) {
		this.getLoginId_loginSession().put(id, session);
		this.getSessionId_session().put(session.getId(), session);
	}

	@Override
	public void saveWebSocketSession(long id, Session session) {
		this.getLoginId_webSocket().put(id, session);
	}

	// @Override
	// public void removeLoginSession_WebSocketSession(long id) {
	// this.getSessionId_session().remove(
	// this.getLoginId_loginSession().remove(id).getId());
	// }

	@Override
	public void delWebSocketSessionByLid(long id) {
		this.getLoginId_webSocket().remove(id);
	}

	@Override
	public HttpSession getHttpSessionBySid(String sid) {
		return this.getSessionId_session().get(sid);
	}

	@Override
	public void delSessionBySid(String sid) {
		LoginDisplay login = (LoginDisplay) this.getSessionId_session()
				.remove(sid).getAttribute("login");
		this.getLoginId_webSocket().remove(login.DecryptByDisplay());
		this.getLoginId_loginSession().remove(login.DecryptByDisplay());
	}

	// @Override
	// public Long getLoginIdBySession(HttpSession session) {
	// return this.getLoginSession_loginId().get(session);
	// }

}
